﻿using AutoMapper;
using Microsoft.AspNet.Identity.EntityFramework;
using Stackoverflow.Core.Base;
using Stackoverflow.Core.Questions;
using Stackoverflow.Web.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace Stackoverflow
{
    public class MappingConfig
    {
        public static void RegisterMappings()
        {
            Mapper.CreateMap<Entity, BaseModel>()
                 .ForMember(des => des.CreatedDate, opt => opt.MapFrom(src => src.CreatedDate.HasValue ? src.CreatedDate.Value.ToString("MM/dd/yyyy HH:mm:ss") : ""))
                 .ForMember(des => des.UpdatedDate, opt => opt.MapFrom(src => src.UpdatedDate.HasValue ? src.CreatedDate.Value.ToString("MM/dd/yyyy HH:mm:ss") : ""));

            Mapper.CreateMap<BaseModel, Entity>()
                .ForMember(des => des.CreatedDate, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.CreatedDate) ? DateTime.ParseExact(src.CreatedDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture) : (DateTime?)null))
                .ForMember(des => des.UpdatedDate, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.UpdatedDate) ? DateTime.ParseExact(src.UpdatedDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture) : (DateTime?)null));

            Mapper.CreateMap<QuestionModel, Question>().ReverseMap();

        }
    }
}